package com.be.beadmin.device.controller;

import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.device.entity.SpareFile;
import com.be.beadmin.device.dto.output.SpareFileEx;
import com.be.beadmin.project.Util.MinioUtil;
import com.be.beadmin.service.impl.MinioService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Page;
import com.be.beadmin.device.service.SpareFileService;
import com.be.beadmin.device.dto.input.SpareFileQueryCriteria;
import com.be.beadmin.device.dto.input.SpareFileDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import com.alibaba.excel.EasyExcel;
import org.springframework.web.multipart.MultipartFile;

/**
* @author zty
* @date 2023-10-17
**/
@RestController
public class SpareFileController {

    @Autowired
    private  SpareFileService spareFileService;
    @Resource
    private MinioService minioService;

    MinioUtil minioUtil = new MinioUtil();

    private String bucketName = "sparefile";


    @PostMapping(value = "/device/getSpareFileByPage")
    public ResultModel getSpareFileByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(spareFileService.getSpareFileByPage(pageEntity)) ;
    }

    @PostMapping("/device/addSpareFile")
    public ResultModel addSpareFile(@RequestBody SpareFileDto spareFileDto){
        spareFileService.addSpareFile(spareFileDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/device/delSpareFile")
    public ResultModel delSpareFile(@RequestBody SpareFileDto spareFileDto){
        spareFileService.delSpareFile(spareFileDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/device/updateSpareFile")
    public ResultModel updateSpareFile(@RequestBody SpareFileDto spareFileDto){
        spareFileService.updateSpareFile(spareFileDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/device/exportSpareFile")
    public void exportSpareFile(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<SpareFileEx> spareFilePage = spareFileService.getSpareFileByPage(pageEntity);
        List<SpareFileEx> spareFileExes = spareFilePage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("SpareFile接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), SpareFileEx.class).sheet("sheet1").doWrite(spareFileExes);
    }

    @ApiOperation("新增附件信息")
    @PostMapping("/device/uploadSpareFile")
    public ResultModel uploadSpareFile(
            @ApiParam(value = "附件",required = true) @RequestParam("file") MultipartFile[] multipartFiles,
            @ApiParam(value = "spareinfo_id",required = true) @RequestParam String spareinfoId,
            @ApiParam(value = "文件名称") @RequestParam String fileName) throws Exception {

        spareFileService.uploadSpareFile(spareinfoId,multipartFiles,fileName,bucketName);
        return ResultModel.success("添加附件成功");
    }
}